package com.nimble.smile.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nimble.smile.R;
import com.nimble.smile.common.CommonAdapter;
import com.nimble.smile.service.OperationCode;
import com.nimble.smile.service.vo.EmotionDTO;
import com.nimble.smile.service.vo.UserInfo;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by simon on 17-5-5.
 */

public class RankActivity extends BaseActivity {

    @Bind(R.id.rank_list)
    ListView rankList;

    @Bind(R.id.SecondPlaceImage)
    ImageView secondPlaceImageView;
    @Bind(R.id.SecondPlaceName)
    TextView secondPlaceNameTextView;

    @Bind(R.id.FirstPlaceImage)
    ImageView firstPlaceImageView;
    @Bind(R.id.FirstPlaceName)
    TextView firstPlaceNameTextView;

    @Bind(R.id.ThirdPlaceImage)
    ImageView thirdPlaceImageView;
    @Bind(R.id.ThirdPlaceName)
    TextView thirdPlaceNameTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank);
        ButterKnife.bind(this);

        initData();
    }

    private void initData() {
        showProgressDialog("加载中...");
        subscribe(getService().getRankList(), response -> {
            hideProgressDialog();
            if (response.getOperationCode() == OperationCode.SUCCESS.getValue()) {
                List<EmotionDTO> content = response.getBody().getContent();
                rankList.setAdapter(new RankAdapter(this, content));

                if (content != null && content.size() > 0){
                    loadPerson(content.get(0), firstPlaceImageView, firstPlaceNameTextView);
                    if (content.size() > 1){
                        loadPerson(content.get(1), secondPlaceImageView, secondPlaceNameTextView);
                    }
                    if (content.size() > 2){
                        loadPerson(content.get(2), thirdPlaceImageView, thirdPlaceNameTextView);
                    }
                }
            } else {
                showToast("加载失败...");
            }
        });
    }

    private void loadPerson(EmotionDTO content, ImageView iv, TextView tv) {
        EmotionDTO firstPlace = content;
        String key = firstPlace.getImg();
        Picasso.with(this)
                .load("http://kanken.mynimble.cn/" + key)
                .placeholder(R.mipmap.photo_preview_holder)
//                            .resizeDimen(R.dimen.image_preview_size, R.dimen.image_preview_size)
                .into(iv, new Callback() {
                    @Override
                    public void onSuccess() {

                    }

                    @Override
                    public void onError() {

                    }
                });
        tv.setText(firstPlace.getUserInfo().getName());
    }

    @OnClick(R.id.back)
    public void onBackClick(View view){
        onBackPressed();
    }

    private class RankAdapter extends CommonAdapter<EmotionDTO> {

        RankAdapter(Context context, List<EmotionDTO> mData) {
            super(context, mData);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_rank, parent, false);
                convertView.setTag(new ViewHolder(convertView));
            }

            ViewHolder holder = (ViewHolder) convertView.getTag();
            EmotionDTO item = getItem(position);
            Long rank = item.getRank();
            if (rank <= 3){
                int resId = -1;
                if (rank == 1){
                    resId = R.drawable.first_with_text;
                } else if (rank == 2){
                    resId = R.drawable.second_with_text;
                }else{
                    resId = R.drawable.third_with_text;
                }

                holder.imageView.setImageResource(resId);
                holder.imageView.setVisibility(View.VISIBLE);
                holder.rank.setVisibility(View.GONE);

                holder.score.setTextColor(RankActivity.this.getResources().getColor(R.color.modal_score_color));
            }else {
                holder.rank.setVisibility(View.VISIBLE);
                holder.rank.setText(String.valueOf(rank));

                holder.imageView.setVisibility(View.GONE);
                holder.score.setTextColor(RankActivity.this.getResources().getColor(R.color.score_color));
            }

            holder.score.setText(String.valueOf(item.getScore()));

            UserInfo userInfo = item.getUserInfo();
            if (userInfo != null ) {
                String location = userInfo.getLocation();
                holder.department.setText(!TextUtils.isEmpty(location) ? location : "游客");
                holder.name.setText(item.getUserInfo().getName());
            }

            return convertView;
        }
    }

    static class ViewHolder {
        @Bind(R.id.rank_for_brand)
        ImageView imageView;
        @Bind(R.id.rank)
        TextView rank;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.score)
        TextView score;
        @Bind(R.id.department)
        TextView department;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

}
